
var parentJson = {};
parentJson.bakUrl="http://localhost:8081/unite_platform/meeting_info/toSelect";
parentJson.jsType="add";
parentJson.parameter="";
parentJson.showType="";
var now = getDay(0); 
var username = sessionStorage.getItem("name");
var userid = sessionStorage.getItem("userId");
var dingdingUserData;
var result;
var thenum = 0;
var itemJson = {};
function ICalendarClick(){
	
}
$(document).ready(function(){
/*	alert(sessionStorage.getItem("reload"));*/
    if(sessionStorage.getItem("reload")=='true'){
	     window.location.reload();
		sessionStorage.setItem("reload", false);
		console.log("reload"); 
	}else{
		getPandianBitian();
		addoperation();
		
		$('#name').text(sessionStorage.getItem("name"));
		$('#searchDate').val(now);
		$('#addButton').on('click',addform);
		$('#saveButton').on('click',saveInfo);
		$('#searchButton').on('click',getPandianBitian);
		$(document).on('click','div[deleteIndex]',function(){
			var obj = this;
//			editInfo($(obj).attr('deleteIndex'),'delete')
			dd.ready(function(){
				dd.device.notification.confirm({
					message: "确认删除此条款吗？",
					title: "提示",
					buttonLabels: ['是', '否'],
					onSuccess : function(result) {
						//onSuccess将在点击button之后回调
						/*
			        {
			            buttonIndex: 0 //被点击按钮的索引值，Number类型，从0开始
			        }
						 */
						if(result.buttonIndex == 0){
//							$('div[itemDiv="'+$(obj).attr('deleteIndex')+'"]').remove();
							editInfo($(obj).attr('deleteIndex'),'delete')
						}
					},
					onFail : function(err) {}
				});
			})
		});
		$(document).on('click','div[editIndex]',function(){
			var obj = this;
//			editInfo($(obj).attr('editIndex'),'edit')
			dd.ready(function(){
				dd.device.notification.confirm({
					message: "确认编辑此条款吗？",
					title: "提示",
					buttonLabels: ['是', '否'],
					onSuccess : function(result) {
						//onSuccess将在点击button之后回调
						/*
			        {
			            buttonIndex: 0 //被点击按钮的索引值，Number类型，从0开始
			        }
						 */
						if(result.buttonIndex == 0){
//							$('div[itemDiv="'+$(obj).attr('deleteIndex')+'"]').remove();
							editInfo($(obj).attr('editIndex'),'edit')
						}
					},
					onFail : function(err) {}
				});
			})
		});
	
	}
	
	
});
function getPandianBitian(){
	showloading();
	var sendData = {};
	sendData.flag = $('#itemflag').val();
	sendData.itemname = $('#itemname').val();
	$.ajax({
		url: "getPandianShopDaiban"
		,type:'post'
		,dataType:'json'
		,async: false
		,contentType: "application/json; charset=utf-8"
		,data:JSON.stringify(sendData) 
		,success: function(res){
			console.log(res)
			try {
				if(res.success){
					var data = res.data;
					thenum = 0;
					$('#f_div').empty();
					if(data == undefined || data.length < 1){
						addform();	
					}else{
						addPandianBitian(data);
					}
				}else{
					layer.msg(res.message);
				}
			} catch (e) {
				console.log(e)
			} finally{
				hideloading();
			}
		}
	    });
}
function saveInfo(){
	showloading();
	try {
		var imgStr = "";
		var sendData = {};
		var insertArray = [];
		var checkFlag = true;
		$('div[itemDiv]').each(function(i,obj){
			var insertJson = {};
			if(checkFlag){
				$(obj).find('[jsonKey]').each(function(j,el){
					if($(el).parent().find('span').text() == '*' && $(el).val() == ''){
						alert($(el).parent().find('h5').text()+"为必填项");
						checkFlag = false;
						return false;
					}else{
						var key = $(el).attr('jsonKey');
						insertJson[key] = $(el).val();
						//console.log(insertJson);
						key = null;
					}
				});
				if(insertJson.id == undefined){
					insertArray.push(insertJson);
				}
			}else{
				return false;
			}
		});
		console.log(sendData);
		if(checkFlag && insertArray.length > 0){
			sendData.dataArray = insertArray;
			sendData.userId=sessionStorage.getItem("userId");
			$.ajax({
				url: "insertPandianShopDaiban"
				,type:'post'
				,dataType:'json'
				,async: false
				,contentType: "application/json; charset=utf-8"
				,data:JSON.stringify(sendData) 
				,success: function(res){
					console.log(res)
					if(res.success){
						location.reload();
					}else{
						hideloading();
						layer.alert(res.message);
					}
				}
		    });
		}
/*		console.log(insertArray.length);*/
	} catch (e) {
		console.log(e)
	} finally{
		hideloading();
	}	
	
}	

function editInfo(itemIndex,opeType){
	var itemDiv = $('div[itemDiv="'+itemIndex+'"]');
	var id = itemDiv.find('input[jsonKey="id"]');
	
	var insertJson = {};
	//证明是可以编辑的，删除也需要从后台删除，不能真正删除，修改为禁用就行
	if(id.length == 1){
		var checkFlag = true;
		$(itemDiv).find('[jsonKey]').each(function(j,el){
			if($(el).parent().find('span').text() == '*' && $(el).val() == ''){
				layer.alert($(el).parent().find('h5').text()+"为必填项");
				checkFlag = false;
				return false;
			}else{
				var key = $(el).attr('jsonKey');
				insertJson[key] = $(el).val();
				//console.log(insertJson);
				key = null;
			}
		});
		if(checkFlag && insertJson.id != ''){
			showloading();
			if(opeType == 'delete'){
				insertJson.flag = 1;
				$('div[itemDiv="'+itemIndex+'"]').remove();
			}
			insertJson.userId=sessionStorage.getItem("userId");
			$.ajax({
				url: "editPandianShopDaiban"
				,type:'post'
				,dataType:'json'
				,async: false
				,contentType: "application/json; charset=utf-8"
				,data:JSON.stringify(insertJson) 
				,success: function(res){
					if(res.success){
						location.reload();
					}else{
						hideloading();
						layer.alert(res.message);
					}
				}
		    });
		}else{
			return false;
		}
	}else{
		$('div[itemDiv="'+itemIndex+'"]').remove();
	}
}

function addform(){
	var itemDivIndex = $('div[itemDiv]').length;
    $('#f_div').append('<div class="col_box" itemDiv="'+thenum+'">'
    					+'<div class="widget-header widget-header-flat widget-header-small add_pad_tb10p add_mar_tb7p">'
						+'<h5 id="wentishuomingTitle" class="black">待办事项'+(itemDivIndex+1)+'</h5>'
						+'<div deleteIndex="'+thenum+'" class="bt_delete bg_orange">删除</div><div class="clearfix"></div>'
						+'</div>'
						+'		<div class="widget-box add_mar_tb7p">'
						+'			<div class="widget-header widget-header-flat widget-header-small add_pad_tb10p add_bor_tbgrey">'
						+'				<h5><i class="iconfont">&#xe600;</i>&nbsp;序号</h5><span class="red">*</span>'
						+'				<input type="text" id="sort'+thenum+'" placeholder="请输入序号" jsonKey="sort" autocomplete="off"   class="pull-right add_bor_none" style="width: 70%;margin-right: 2%"/>'
						+'			</div>'
						+'		</div>'
						+'		<div class="widget-box add_mar_tb7p">'
						+'			<div class="widget-header widget-header-flat widget-header-small add_pad_tb10p add_bor_tbgrey">'
						+'				<h5><i class="iconfont">&#xe600;</i>&nbsp;待办事项</h5><span class="red">*</span>'
						+'				<input type="text" id="itemname'+thenum+'" placeholder="请输入待办事项" jsonKey="itemname" autocomplete="off"   class="pull-right add_bor_none" style="width: 70%;margin-right: 2%"/>'
						+'			</div>'
						+'		</div>'
						+'		<div class="widget-box add_mar_tb7p">'
						+'			<div class="widget-header widget-header-flat widget-header-small add_pad_tb10p add_bor_tbgrey">'
						+'				<h5><i class="iconfont">&#xe600;</i>&nbsp;状态</h5><span class="red"></span>'
						+'				<select id="flag'+thenum+'" jsonKey="flag" class="pull-right add_bor_none" style="width: 70%;margin-right: 2%;">'
						+'				 <option value ="0">启用</option>'
  						+'				<option value ="1">禁用</option>'
						+'				</select> ' 
						+'			</div>'
						+'		</div>'
						+'</div>');					
	
    thenum++;
}

function addPandianBitian(jsonparse){
	for(var i = 0 ;i<jsonparse.length;i++){
	var itemDivIndex = $('div[itemDiv]').length;
	$('#f_div').append('<div class="col_box" itemDiv="'+thenum+'">'
			+'<div class="widget-header widget-header-flat widget-header-small add_pad_tb10p add_mar_tb7p">'
			+'<h5 id="wentishuomingTitle" class="black">待办事项'+(itemDivIndex+1)+'</h5>'
			+'<div deleteIndex="'+thenum+'" class="bt_delete bg_orange">删除</div><div class="clearfix"></div>'
			+'</div>'
			+'		<div class="widget-box add_mar_tb7p">'
			+'			<div class="widget-header widget-header-flat widget-header-small add_pad_tb10p add_bor_tbgrey">'
			+'				<h5><i class="iconfont">&#xe600;</i>&nbsp;序号</h5><span class="red"></span>'
			+'				<input type="text" id="sort'+thenum+'" value="'+jsonparse[i].sort+'" placeholder="请输入条款序号" jsonKey="sort" autocomplete="off"   class="pull-right add_bor_none" style="width: 70%;margin-right: 2%"/>'
			+'			</div>'
			+'		</div>'
			+'		<div class="widget-box add_mar_tb7p">'
			+'			<div class="widget-header widget-header-flat widget-header-small add_pad_tb10p add_bor_tbgrey">'
			+'				<h5><i class="iconfont">&#xe600;</i>&nbsp;待办事项</h5><span class="red"></span>'
			+'				<input type="text" id="name'+thenum+'" value="'+jsonparse[i].itemname+'" placeholder="请输入待办事项" jsonKey="itemname" autocomplete="off"   class="pull-right add_bor_none" style="width: 70%;margin-right: 2%"/>'
			+'			</div>'
			+'		</div>'
			+'		<div class="widget-box add_mar_tb7p">'
			+'			<div class="widget-header widget-header-flat widget-header-small add_pad_tb10p add_bor_tbgrey">'
			+'				<h5><i class="iconfont">&#xe600;</i>&nbsp;状态</h5><span class="red"></span>'
			+'				<select id="flag'+thenum+'" jsonKey="flag" class="pull-right add_bor_none" style="width: 70%;margin-right: 2%;">'
			+'				 <option value ="0">启用</option>'
				+'				<option value ="1" '+(jsonparse[i].flag=='1'?"selected":"")+'>禁用</option>'
			+'				</select> ' 
			+'			</div>'
			+'		</div>'
			+'<input type="hidden" id="id'+thenum+'" value="'+jsonparse[i].id+'" jsonKey="id"/>'
			+'<div editIndex="'+thenum+'" class="bt_delete bg_blue">编辑</div><div class="clearfix"></div>'					
			+'</div>');

			thenum++;
	}
}


function addoperation(){
	$('#addButton').append(
		'<div class="submit_button w94 bg_red">'
	   +'<span  style="display:inline-block; width:16px; height:16px!important; line-height:14px!important; font-weight:600; font-size:15px; text-align:center; border:1px #fff solid; border-radius:10px; color:#fff!important;">+</span>'
  	   +'新增待办事项</div>'
	);
	
	 $('#saveButton').append('<div class="submit_button w94">提交</div> ');
}


